/****************************************************************************
 * 2025.10 XLB-20250122JTG
 ****************************************************************************/

using System;
using System.Collections.Generic;
using QAssetBundle;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;

namespace ProjectSurvivor
{
	public partial class AchievementController : UIElement
	{
		ResLoader mResLoader = ResLoader.Allocate();
		private void Awake()
		{
			var originLocalPosY = AchievementItem.LocalPositionY();
			
			var iconAtlas = mResLoader.LoadSync<SpriteAtlas>("icon");
			AchievementItem.Hide();
			
			AchievementSystem.OnAchievementUnlocked.Register(item =>
			{
				Title.text = $"<b>成就 {item.Name} 达成！</b>";
				Description.text = item.Description;
				Icon.sprite = iconAtlas.GetSprite(item.IconName);
				
				AudioKit.PlaySound(Sfx.ACHIEVEMENT);
				AchievementItem.LocalPositionY(-200);
				AchievementItem.Show();

				ActionKit.Sequence()
					.Lerp(-200, originLocalPosY, 0.3f, y =>
					{
						AchievementItem.LocalPositionY(y);
					})
					.Delay(2)
					.Lerp(originLocalPosY, -200, 0.3f, y =>
					{
						AchievementItem.LocalPositionY(y);
					}, () =>
					{
						AchievementItem.Hide();
					})
					.Start(this);


			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}

		protected override void OnBeforeDestroy()
		{
			mResLoader.Recycle2Cache();
			mResLoader = null;
		}
	}
}